package com.rois.sample.CompReaction;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;

import com.rois.librois.libRoISComp.*;
import com.rois.librois.libRoISComp.CommandControl;

public class ReactionMain {
	private static libRoISComp lib;
	private static ExecuteOperationImpl exec;
	private static ComponentEntityImpl comp;
	private static CommandControl cmd;
	private static Window window;

	public static void main(String[] args)
	{

		System.out.println("please input [start] to start");

		// create libRoISComp class
		lib = new libRoISComp();
		// create execute operation class
		exec = new ExecuteOperationImpl();
		// create component entity class
		comp = new ComponentEntityImpl(lib, exec);
		// create command control class
		cmd = new CommandControl(lib, comp);
		// create window class
		window = new Window(cmd);
		exec.setWindow(window);

		boolean bRun = false;
		String s = null;
		BufferedReader br = new BufferedReader(new InputStreamReader(System.in), 1);
/*
//@@ wait start command input
		while(!bRun)
		{
			try {
				s = br.readLine();
				if(s.matches("help"))
				{
		 			System.out.println("please input:");
					System.out.println(" start");
					System.out.println(" stop");
				}
				else if(s.matches("start"))
				{
					if(cmd.start(comp))
					{
						bRun = true;
					}
					else
					{
			 			System.out.println("Failed to start.");
					}
				}
				else if(s.matches("stop"))
				{
					break;
				}
				else
				{
					System.out.println("please input [start] to start");
				}
			} catch (IOException e) {
				e.printStackTrace();
			}
		}
*/
//@@ start automatically
		if( cmd.start(comp) && cmd.connect() )
		{
			bRun = true;
		}

		while(bRun)
		{
			try {
				s = br.readLine();
				if(s.matches("help"))
				{
		 			System.out.println("please input:");
					System.out.println(" stop");
					System.out.println(" disconnect");
					System.out.println(" completed,[command ID],[command status]");
					System.out.println(" receive_event,[event type],[subscribe ID]");
					System.out.println(" receive_error,[error type]");
					System.out.println(" show connect");
					System.out.println(" show bind");
					System.out.println(" show command");
					System.out.println(" show command result");
					System.out.println(" show event");
					System.out.println(" show event result");
					System.out.println(" show error");
				}
				else if(s.matches("stop"))
				{
					cmd.stop();
					bRun = false;
				}
				else if(s.matches("connect"))
				{
					if(!cmd.connect())
					{
			 			System.out.println("Failed to connect.");
					}
				}
				else if(s.matches("disconnect"))
				{
					if(!cmd.disconnect())
					{
			 			System.out.println("Failed to disconnect.");
					}
				}
				else if(s.contains("completed"))
				{
					String[] strseq = s.split(",");
					if(strseq.length<3)
					{
			 			System.out.println("please input command ID and status after ','");
					}
					else
					{
						cmd.sendCompleted(strseq[1], strseq[2]);
					}
				}
				else if(s.contains("receive_event"))
				{
					String[] strseq = s.split(",");
					if(strseq.length<3)
					{
			 			System.out.println("please input event type and subscribe ID after ','");
					}
					else
					{
						cmd.sendEvent(strseq[1], strseq[2]);
					}
				}
				else if(s.contains("receive_error"))
				{
					String[] strseq = s.split(",");
					if(strseq.length<2)
					{
			 			System.out.println("please input error type after ',':");
			 			System.out.println("[comp] [response] [engine] [user]");
					}
					else
					{
						cmd.sendError(strseq[1]);
					}
				}
				// for Debug
				else if(s.matches("show connect"))
				{
					if(!cmd.showConnectEngineList())
					{
			 			System.out.println("No Engine is connected.");
					}
				}
				else if(s.matches("show bind"))
				{
					if(!cmd.showBindEngineList())
					{
			 			System.out.println("No Engine is bound.");
					}
				}
				else if(s.matches("show command"))
				{
					if(!cmd.showRequestedCommandList())
					{
			 			System.out.println("No Command is requested.");
					}
				}
				else if(s.matches("show command result"))
				{
					if(!cmd.showCompletedCommandList())
					{
			 			System.out.println("No Command is completed.");
					}
				}
				else if(s.matches("show event"))
				{
					if(!cmd.showSubscribeEventList())
					{
			 			System.out.println("No Event is subscribed.");
					}
				}
				else if(s.matches("show event result"))
				{
					if(!cmd.showSentEventList())
					{
			 			System.out.println("No Event is published.");
					}
				}
				else if(s.matches("show error"))
				{
					if(!cmd.showSentErrorList())
					{
			 			System.out.println("No Error is sent.");
					}
				}
				else
				{
					System.out.println("please input interface method");
				}
			} catch (IOException e) {
				e.printStackTrace();
			}
		}
		try {
			br.close();
		} catch (IOException e) {
			e.printStackTrace();
		}
		System.out.println("finished.");
		window.close();
		return;
	}
}
